home *** CD-ROM | disk | FTP | other *** search
- /* CROND & CRONTAB: (c) Kees Lemmens, Netherlands; June 1993.
-
- Programs for ATARI ST (running under MINT) to make it possible
- to run background jobs at regular intervals.
-
- Any questions or suggestions about this program can be send to:
- lemmens@dv.twi.tudelft.nl
- */
-
- #include "cron.h"
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #include <signal.h>
- #include <errno.h>
- #include <stdarg.h>
-
- char *ux2dos(char *string) /* convert / to \ */
- { char *tmp;
- while((tmp=strchr(string,'/')) != NULL)
- *tmp='\\';
- return string;
- }
-
- void LogMsg(char *name,int pid,char *fmt,...)
- {
- /* If LOGFILE is defined this function writes to the */
- /* appropriate file; otherwise, it does nothing. */
-
- #if defined(LOGFILE) || defined(DEBUG)
- va_list ptr;
- char *msg;
- long now = time((long *) 0);
- char *ti = ctime(&now);
- FILE *log_fd;
-
- msg = malloc(strlen(name) + strlen(fmt) + MAX_TEMPSTR);
- va_start(ptr,fmt);
-
- sprintf(msg, "%s (%.3d) %.24s: %s\n", name, pid, ti, fmt);
-
- #ifdef DEBUG
- # ifdef LOGFILE
- # undef LOGFILE
- # endif
- # define LOGFILE "u:/dev/tty"
- #endif
-
- if((log_fd = fopen(ux2dos(LOGFILE), "a")) == NULL)
- { fprintf(stderr,"%s: can't open log file\n",name);
- perror(LOGFILE);
- return;
- }
- if(vfprintf(log_fd,msg,ptr) == EOF)
- { fprintf(stderr,"%s: can't write to log file\n",name);
- perror(LOGFILE);
- fprintf(stderr,msg);
- }
- fclose(log_fd);
-
- free(msg);
- va_end(ptr);
-
- #endif /*LOGFILE */
- }
-
- /* force a crontabs reread by sending a signal to CROND daemon */
-
- void PokeDaemon(char cmd)
- { int pipe;
-
- if((pipe=open(ux2dos(CRONPIPE),O_WRONLY)) >= 0)
- { if(write(pipe,&cmd,1) == 1)
- { close(pipe);
- return;
- }
- }
- close(pipe);
- fputs(PROGNAME " : can't send signal to daemon\n",stderr);
- return;
- }
-
- void AlarmHandler(void)
- { return;
- }
-
- void cronsleep(unsigned long seconds)
- {
- /* Avoid heavy system load while waiting for input :
- Standard sleep functions in PURE C cause enormous load on a
- multitasking system, as they are stupid loops !!
- */
- long mask=sigblock(0L); /* get current mask */
-
- signal(SIGALRM,AlarmHandler);
-
- #ifdef DEBUG
- printf("\nMasked signals: ");
- PrintBin(31,sigblock(0L));
- putchar('\n');
- #else
- sigsetmask(mask & ~((1L<<SIGINT)|(1L<<SIGTERM)));
- /* unmask blocked signals */
- #endif
-
- alarm(seconds); /* set alarm time */
- (void)pause(); /* wait for alarm */
-
- sigsetmask(mask); /* restore old mask when not sleeping */
- }
-